package com.lz.ui_lib;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import com.google.gson.Gson;
import com.lz.ui_lib.adapter.PagerAdapter;
import com.lz.ui_lib.bean.ActivityInfos;
import com.lz.ui_lib.widget.MyGridView;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

/**
 * UI组件入口
 * <p>
 * Created by liuyakui on 2022/2/13.
 */
public class UiMainActivity extends FragmentActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
    private MyGridView mGridView;
    private List<ActivityInfos> mActivityInfos;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ui_activity_main);

        mGridView = findViewById(R.id.ui_grid_view);

        mActivityInfos = parseJson(this, "config/ui_home_config", ActivityInfos[].class);
        PagerAdapter mAdapter = new PagerAdapter(this);
        mAdapter.setItems(mActivityInfos);
        mGridView.setAdapter(mAdapter);
        mGridView.setOnItemClickListener(this);
        mGridView.setOnItemLongClickListener(this);
        mGridView.setAdapter(mAdapter);
        mGridView.setItems(mActivityInfos);

        findViewById(R.id.f_over).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                unedited();
            }
        });
    }

    public void goToFunctionPager(int position) {
        goToFunctionPager(this, mActivityInfos.get(position));
    }

    public void goToFunctionPager(Context context, ActivityInfos activityInfos) {
        final Intent intent = initIntent(activityInfos.getIntentUrl());
        context.startActivity(intent);
    }

    private Intent initIntent(final String uriString) {
        final Intent intent = new Intent();
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(uriString));
        return intent;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//        goToFunctionPager(position);

        Intent intent = new Intent(this, UiTestActivity.class);
        intent.putExtra("extra", mActivityInfos.get(position));
        startActivity(intent);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        mGridView.builderDragView();
        editing();
        return true;
    }

    private void editing() {
        mGridView.setDragEnabled(true);
    }

    private void unedited() {
        mGridView.setDragEnabled(false);
    }

    public <T> List<T> parseJson(Context context, String fileName, Class<T[]> clazz) {
        try {
            final InputStream in = context.getResources().getAssets().open(fileName);
            final String jsonText = new String(getString(in));
            final Gson gson = new Gson();
            List<T> list = null;

            if (!TextUtils.isEmpty(jsonText)) {
                final T[] t = gson.fromJson(jsonText, clazz);
                list = Arrays.asList(t);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public byte[] getString(InputStream in) {
        ByteArrayOutputStream byo = new ByteArrayOutputStream();
        try {
            byte[] buff = new byte[1024];
            int len;
            while ((len = in.read(buff)) != -1) {
                byo.write(buff, 0, len);
                byo.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null)
                try {
                    in.close();
                    byo.close();
                } catch (IOException ie) {
                    ie.printStackTrace();
                }
        }
        return byo.toByteArray();
    }
}